import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:xiongan_app_yy/common/user_singleton.dart';
import 'package:xiongan_app_yy/provider/message_provider.dart';

//消息页
class MessagePage extends StatefulWidget {
  @override
  _MessagePageState createState() => _MessagePageState();
}

class _MessagePageState extends State<MessagePage> {
  List<dynamic> _data = List();

  Future<void> _refresh() async {
    await Future.delayed(Duration(seconds: 1), () {
      return null;
    });
    Provider.of<MessageProvider>(context).getList();
  }

  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addPostFrameCallback((_) async {
      Provider.of<MessageProvider>(context).getList();
    });
  }

  @override
  Widget build(BuildContext context) {
    _data = Provider.of<MessageProvider>(context).listData;
    return Scaffold(
      appBar: new AppBar(
        backgroundColor: Color(0XFF00A5F1),
        automaticallyImplyLeading: false,
        //设置没有返回按钮
        title: Text(
          "消息",
          style: TextStyle(fontSize: 18),
        ),
        elevation: 0,
        centerTitle: true,
      ),
      body: _data.length != 0
          ? RefreshIndicator(
              child: ListView.separated(
                  itemBuilder: (BuildContext context, int index) {
                    return MessageItem(_data[index], () {
                      Provider.of<MessageProvider>(context)
                          .sendMessageInfo(_data[index]["id"]);
                      _refresh();
                      //状态判断 1  类型判断   其他： 消息详情
                      if (DataSingleton().Data.appModuleMenusList.length == 1) {
                        //通知类型 10 入库任务,20 出库任务,30 退货任务,40 退库任务,50 调配,60 配送
                        switch (_data[index]["notifytype"]) {
                          case '10':
                            Navigator.pushNamed(context, "/invListItemDetails",
                                arguments: {
                                  "id": _data[index]["taskid"],
                                  "type": "入库预约单",
                                  "state": "审核通过"
                                });
                            break;
                          case '20':
                            Navigator.pushNamed(context, "/invListItemDetails",
                                arguments: {
                                  "id": _data[index]["taskid"],
                                  "type": "出库预约单",
                                  "state": "审核通过"
                                });
                            break;
                          case '30':
                            break;
                          case '40':
                            Navigator.pushNamed(
                                context, "/return_booking_detail", arguments: {
                              "id": _data[index]["taskid"],
                              "type": "工程结余退库预约单",
                              "state": "审核通过"
                            });
                            break;
                          case '50':
                            Navigator.pushNamed(
                                context, '/deploymentListDetailsNew',
                                arguments: {
                                  "id": _data[index]["taskid"],
                                  "type": "调配预约单",
                                  "state": "审核通过"
                                });
                            break;
                          case '60':
                            Navigator.pushNamed(context, "/deliveryListDetails",
                                arguments: {
                                  "id": _data[index]["taskid"],
                                  "type": "配送预约单",
                                  "state": "审核通过"
                                });
                            break;
                          default:
                            Navigator.pushNamed(context, '/message_details',
                                arguments: _data[index]);
                        }
                      } else {
                        Navigator.pushNamed(context, '/message_details',
                            arguments: _data[index]);
                      }
                    });
                  },
                  separatorBuilder: (BuildContext context, int index) {
                    return Divider(
                      color: Colors.black26,
                      height: 1,
                    );
                  },
                  itemCount: _data.length),
              onRefresh: _refresh)
          : Center(
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: <Widget>[
                  Image.asset(
                    "assets/images/empty.png",
                    fit: BoxFit.fill,
                    width: 100,
                    height: 80,
                  ),
                  Container(height: 10,),
                  Text(
                    "暂无消息",
                    style: TextStyle(fontSize: 14),
                  )
                ],
              ),
            ),
    );
  }
}

class MessageItem extends StatelessWidget {
  dynamic data;
  Function() onTap;

  MessageItem(this.data, this.onTap);

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      child: Container(
        color: Colors.white,
        padding: EdgeInsets.all(16),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: <Widget>[
            _Text("${data['creationTime']}"),
            Container(
              width: double.infinity,
              padding: EdgeInsets.only(top: 8),
              child: Text(
                "${data['note']}",
                maxLines: 1,
                overflow: TextOverflow.ellipsis,
                style: TextStyle(
                  color: Color(0xff646464),
                  fontSize: 14,
                ),
              ),
            )
          ],
        ),
      ),
      onTap: onTap,
    );
  }
}

class _Text extends Text {
  TextStyle style;

  _Text(String data,
      {this.style = const TextStyle(
        color: Color(0xff646464),
        fontSize: 14,
      )})
      : super(data, style: style);
}
